<style>
a#xl{position:fixed;right:2px;top:2px;}
.compacted{color:red;}
</style>

<a id="xl"  href="javascript:void(0);" onclick="parent.closeSearchRes();">[X]</a>

<?php
	// finds all *.big.* files and compact them
	// removes comments
	// puts all functions on one line
	$compact_files= array(
		"^\./.*\.big\..*$"
	);
	compactDir("./");
	//////////////////////////////////////////////////////////////
	// End of configuration part
	//////////////////////////////////////////////////////////////

	function compactDir($dirpath){
//		debug("compacting dir : ".$dirpath);
		$dir   = opendir($dirpath);
		while ($ff=readdir($dir)) {
			if ($ff!=".." && $ff!=".") {
				$ff = $dirpath . $ff;
				if (is_dir($ff)) {
					// compactDir($ff."/");
				} else{
					if (hastocompact($ff)) {
						compactFile($ff);
					}
				}
			}
		}
		closedir($dir);
	}
	function compactFile($file) {
		debug("<span class='compacted'>compacting file : ".$file);
		// read
		$fp = fopen($file, "r");
		$file_content = fread($fp, filesize($file));
		fclose($fp);
		// transform
		$fileparts = split("\.", $file);
		$matches = array();
		$replaces = array();
		$lasti = sizeof($fileparts)-1;
		if ($fileparts[$lasti] == "js") {
			$matches = array(
			"/\/\*[^*]*\*+([^\/*][^*]*\*+)*\//m", // remove comments multiline
			"/^[\s\t]*\/\/.*$/m", // remove only comments line
			"/;[\s\t]*\/\/[^\n]*/", // remove comments after ;
			"/{[\s\t]*\/\/[^\n]*/", // remove comments after {
			"/}[\s\t]*\/\/[^\n]*/", // remove comments after }
			"/[\s\t]+/", // replace multiple spaces by one only
			"/[\s\t]*([{};=\+\(\)&<>])[\s\t]*/", // remove space next to {
			"/[\r\n]/", // remove new line
			"/([^=][\s\t]*)function/" // add new line before function
			);
			$replaces = array(
			"",
			"",
			";",
			"{",
			"}",
			" ",
			"$1",
			"",
			"$1\nfunction"
			);
		} else if ($fileparts[$lasti] == "css") {
			$matches = array(
			"/\/\*[^*]*\*+([^\/*][^*]*\*+)*\//m", // remove comments multiline
			"/^[\s\t]*\/\/.*$/m", // remove only comments line
			"/;[\s\t]*\/\/[^\n]*/", // remove comments after ;
			"/{[\s\t]*\/\/[^\n]*/", // remove comments after {
			"/}[\s\t]*\/\/[^\n]*/", // remove comments after }
			"/[\s\t]+/", // replace multiple spaces by one only
			"/[\s\t]*([{};=\+\(\)&<>])[\s\t]*/", // remove space next to {
			"/[\r\n]/", // remove new line
			"/}/" // add new line after }
			);
			$replaces = array(
			"",
			"",
			";",
			"{",
			"}",
			" ",
			"$1",
			"",
			"}\n"
			);
		}

		$file_content = preg_replace($matches, $replaces, $file_content);

		
		//save
		save($file, $file_content);
	}
	function save($file, $file_content) {
		$tmpfilename = $file.rand().".tmp";
		$fpw = fopen($tmpfilename, "w");
		if (flock($fpw, LOCK_EX)) { 
			// do an exclusive lock
			fwrite($fpw, $file_content);

			flock($fpw, LOCK_UN); // release the lock
			fclose($fpw);  
//			copy($file, $file.'.old');
			$newfilename = getNewFileName($file);
			copy($tmpfilename, $newfilename);
			unlink($tmpfilename);
			$orisize = filesize($file);
			$newsize = filesize($newfilename);
			$perce = round(100-$newsize*100/$orisize);
			debug("\t : ".$perce."%\n</span><br/>");
		} else {
			echo "Couldn't lock the file !";
			fclose($fpw);  
		}

	}
        function getNewFileName($file) {
			$fileparts = split(".big", $file);
			$newfilename = $fileparts[0].$fileparts[1];
            return $newfilename;
        }
	function hastocompact($filename) {
		global $compact_files;
		if (hastocheck($filename, $compact_files)) {
                     $locdate = filemtime($filename);
	             $newfilename = getNewFileName($filename);
                     $newlocdate = filemtime($newfilename);
                     if ($newlocdate < $locdate) {
                       return true;
                     } else  {
                        debug("pas ".$filename."\n<br/>");
                        return false;
                     }
                    
                } else  {
                     return false;
                }
	}
	function hastocheck($filename, $list) {
		for($i=0;$i<sizeof($list);$i++){
			if (ereg($list[$i], $filename)) {
	//			debug("has to check file ".$filename);
				return (!endsWith($filename, ".old"));
			}
		}
		return false;
	}
// return tru if $str ends with $sub
function endsWith( $str, $sub ) {
   return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub );
}
	function debug($s) {
		print $s;
                ob_flush();flush();
	}

	?>

<b>The end</b>
